// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Matrix.ServiceCommunication.Services;

/// <summary>
/// 服务客户端工厂接口
/// </summary>
public interface IServiceClientFactory
{
    /// <summary>
    /// 创建服务HTTP客户端
    /// </summary>
    /// <param name="serviceName">服务名称</param>
    /// <returns>HTTP客户端</returns>
    HttpClient CreateClient(string serviceName);

    /// <summary>
    /// 创建带用户上下文的HTTP客户端
    /// </summary>
    /// <param name="serviceName">服务名称</param>
    /// <param name="userContext">用户上下文</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>HTTP客户端</returns>
    Task<HttpClient> CreateClientAsync(string serviceName, UserContext? userContext = null, CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取服务地址
    /// </summary>
    /// <param name="serviceName">服务名称</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>服务地址</returns>
    Task<Uri> GetServiceAddressAsync(string serviceName, CancellationToken cancellationToken = default);
}

/// <summary>
/// 用户上下文信息
/// </summary>
public class UserContext
{
    /// <summary>
    /// 用户ID
    /// </summary>
    public string UserId { get; set; } = string.Empty;

    /// <summary>
    /// 用户名
    /// </summary>
    public string UserName { get; set; } = string.Empty;

    /// <summary>
    /// 租户ID
    /// </summary>
    public string? TenantId { get; set; }

    /// <summary>
    /// 访问令牌
    /// </summary>
    public string? AccessToken { get; set; }

    /// <summary>
    /// 请求ID
    /// </summary>
    public string? RequestId { get; set; }

    /// <summary>
    /// 调用链ID
    /// </summary>
    public string? TraceId { get; set; }

    /// <summary>
    /// 附加属性
    /// </summary>
    public Dictionary<string, object> Properties { get; set; } = new();
}